#!/bin/zsh
#+++ Description Start +++
# 读取ChangeLog.md中的版本信息
# 写入到plist文件中.
#=== Description End ===

changeLogPath=$1

# 验证changelog文件.
if [[ ! -e "${changeLogPath}" ]]; then
	echo "This project is versioned by change log file,but no change log file found!"
	exit -1
fi
echo "Change log file is valid"


# 验证info.plist,
filePath=$2
if [[ ! -e ${filePath} ]]; then
	echo "filePath is invalid"
	exit -1
fi
echo "filePath is valid"


# 读取版本信息.
export tag="[${TARGET_NAME}]"
# 满足第2个元素=tag的第一行.
currentVersion=$(awk -v tag="${tag}" 'BEGIN {currentTagIndex=0;version="";} {if($2 == tag){currentTagIndex++; if(currentTagIndex==1){version=""$3;version2=substr(version,2,length(version)-2) ;exit}}} END{print version2}' ${changeLogPath})
if [[ ${currentVersion} == "" ]]; then
	echo "invalid version"
	exit -1;
else
	echo "current version is:${currentVersion}"
fi

# 通过seek命令更新到指定文件
# "="号前面的部分,不包括等号,变量名就可以了.
pattern="sdkShortVersion"
# 数字为列数,从1开始.
#build=`eval "awk '/${pattern}/ {print \\$5}' ${filePath}"`
cmd="sed -i \"\" 's/${pattern} = .*/${pattern} = @\"${currentVersion}\";/' ${filePath}"
echo "cmd is: ${cmd}"
eval ${cmd}
[[ $? != 0 ]] && echo "update sdk version failed" && exit -1

